
package com.ifcar.app.activity.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ifcar.app.activity.ImageSwitcherActivity;
import com.ifcar.app.activity.LinkOutSide;
import com.ifcar.app.activity.NewsPage;
import com.ifcar.app.activity.SpecialAritcleTwo;
import com.ifcar.app.activity.newspage.NewsFragment;
import com.ifcar.app.adapter.HeadAdvertismentAdapter;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.model.News;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.BasePageViewAdapter;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.util.Tools;
import com.ifcar.app.view.HeadAdvertismentGallery;
import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.activity.R;

public class NewsListWithHeadlineFragment extends NewsListFragment {
    private final String ADVERTISMENT_JSON_STR = "advertisment_json_string";

    private static final int ADV_COUNT = 2;

    private HeadAdvertismentAdapter mAdvertimentAdapter;

    private List<Map<String, Object>> mHeadAdvertismentData = new ArrayList<Map<String, Object>>();

    private View mHeadAdv;

    private HeadAdvertismentGallery mAdvGallery;

    private Context mContext;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Context context = getActivity();
        mContext = context;
        if (mHeadAdv == null) {
            mHeadAdv = getHeadlineView(LayoutInflater.from(context));
            mAdvGallery = (HeadAdvertismentGallery) mHeadAdv
                    .findViewById(R.id.head_advertisment_grally);

            HashMap<String, Integer> whmap = Tools.getWidth_Height(context);
            int screenwidth = whmap.get("w");
            android.view.ViewGroup.LayoutParams lp = mAdvGallery.getGallery().getLayoutParams();
            lp.height = Tools.getCutHeadPicHeight(screenwidth);
        }
        
        super.onActivityCreated(savedInstanceState);
    }
    
    protected View getHeadlineView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.head_advertisment, null);
    }

    @Override
	public void onLoadingListViewAttached(LoadingListView loadingListView) {
        if (mHeadAdv != null) {
            ListView listView = loadingListView.getListView();
            listView.addHeaderView(mHeadAdv, null, false);
        }
		super.onLoadingListViewAttached(loadingListView);
	}

	@Override
	public void onLoadingListViewDetached(LoadingListView loadingListView) {
		super.onLoadingListViewDetached(loadingListView);
		if (mHeadAdv != null) {
			loadingListView.getListView().removeHeaderView(mHeadAdv);
		}
	}

	@Override
    void loadingNews() {
        // 加载本地数据,没有启用新线程
        List<Map<String, Object>> list = News.getListFromDB(getDb(), mColumnID, "head");
        Message msg = getHandler().obtainMessage(MSG_LOADING);
        msg.obj = list;
        getHandler().handleMessage(msg);
    }

	
    @Override
    protected String getUrlPrefix() {
        return GetColumnsInfo.Headline_list_Url;
    }

    @Override
    void refreshingNews() {
        final String id = mColumnID;
        final String columnUrl = mColumnURL;
        final int num = getLoadingNum();
        final Context context = getActivity();
        final SQLiteDatabase sqldb = getDb();
        new Thread() {
            public void run() {

                List<Map<String, Object>> tmpList = GetColumnsInfo.getHeadlineArticleList(
                        context, getUrlPrefix(), columnUrl, 0, num);

                if (tmpList != null && tmpList.size() > 0) {
                    sqldb.execSQL(
                            "delete from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE
                                    + " where news_col_id='" + id + "'");
                    News.insertListToDB(sqldb, tmpList, id, "head");
                }

                refreshAd(context);

                Message msg = getHandler().obtainMessage(MSG_REFRESHING);
                msg.obj = tmpList;
                getHandler().sendMessage(msg);
            }
        }.start();
    }

    @Override
    void loadingMoreNews(final int start, final int num) {
        final String id = mColumnID;
        final String columnUrl = mColumnURL;
        final Context context = getActivity();
        final SQLiteDatabase sqldb = getDb();
        // 取网络
        new Thread(new Runnable() {
            @Override
            public void run() {

                List<Map<String, Object>> tmpList = GetColumnsInfo.getHeadlineArticleList(context,
                        getUrlPrefix(), columnUrl, start, num);
                
                if(null != tmpList){
                    
                    synchronized (NewsListWithHeadlineFragment.this) {
                        if (mNewsList != null) {
                            removeRepeatedData(mNewsList, tmpList, GetColumnsInfo.DOCID_TAG);
                        }
                    }
                }
                

                Message msg = getHandler().obtainMessage(MSG_LOADING_MORE);
                msg.obj = tmpList;
                getHandler().sendMessage(msg);
            }
        }).start();
    }

    Map<String, Object> first;
    void setData(List<Map<String, Object>> data) {
        if (data != null && data.size() > 0) {
            synchronized (this) {
                cacheDocidListMap(StringUtil.HEAD_TYPE, "0", data, mColumnID);

                first = data.get(0);
                data.remove(0);

                mNewsList = data;

                // 头图添加
                addItemListHeaderView(first);

                mAdapter = new BasePageViewAdapter(getActivity(), data, R.layout.list_item,
                        new String[] {
                                GetColumnsInfo.TITLE_TAG, GetColumnsInfo.DIGEST_TAG,
                                GetColumnsInfo.IMGSRC_TAG, GetColumnsInfo.REPLYCOUNT_TAG
                        }, new int[] {
                                R.id.title, R.id.desc_title, R.id.img, R.id.comment_count
                        }, mImageDownloader);
                mAdapter.setDB(getDb());
                mAdapter.setAdapterType("news");
                ListView listView = getListView();
                if (listView != null) {
                    listView.setAdapter(mAdapter);
                }
            }

        }
    }

    void addData(List<Map<String, Object>> data) {
        if (data == null || data.size() == 0 || mNewsList == null) {
            return;
        }
        mNewsList.addAll(data);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }
    
    protected void refreshAd(Context context) {
        SharedPreferences set_prefs = PreferenceManager
                .getDefaultSharedPreferences(context);

        // 广告头条
        String advertismentString = GetColumnsInfo.getHeadAdvertismentStr(0, ADV_COUNT,
                context);
        if (null != advertismentString && advertismentString.length() > 0) {
            set_prefs.edit().putString(ADVERTISMENT_JSON_STR, advertismentString).commit();
        }
    }

    protected void addItemListHeaderView(Map<String, Object> firstMap) {
        Context context = getActivity();

        HashMap<String, Integer> whmap = Tools.getWidth_Height(context);
        int screenwidth = whmap.get("w");

        int w = screenwidth;
        int h = Tools.getCutHeadPicHeight(screenwidth);
        mHeadAdvertismentData.clear();
        
        mHeadAdvertismentData.add(firstMap);
        
        List<Map<String, Object>> advData = getAdvertismentData(); 
        mHeadAdvertismentData.addAll(advData);
        Bitmap defaultBitmap = null;
        try {
            defaultBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.vote_default)).getBitmap();
        } catch (java.lang.OutOfMemoryError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BaseImageDownloader imageDownloader = new BaseImageDownloader(context,defaultBitmap,w, h, "Y");
        //在wifi条件下进行预加载其余的头图
        if(Tools.isWifi(mContext)){
            preloadHeadPic(imageDownloader,advData);
        }
        mAdvertimentAdapter = new HeadAdvertismentAdapter(context, mHeadAdvertismentData,
                imageDownloader);

        mAdvGallery.setAdapter(mAdvertimentAdapter);

        mAdvGallery.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Map<String, Object> map = mHeadAdvertismentData.get(position);
                // 专题，图集和文章处理，其中第一个区别其他三个
                if (position == 0) {
                    String sdocid = (String) map.get(GetColumnsInfo.SPECIAL_ID_TAG);
                    if (!TextUtils.isEmpty(sdocid)) { // 专题
                        Intent itemintent = new Intent(getActivity(), SpecialAritcleTwo.class);
                        Bundle b = new Bundle();

                        String url = map.get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.URL_TAG));
                        String title = map.get(GetColumnsInfo.TITLE_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.TITLE_TAG));

                        b.putString(GetColumnsInfo.URL_TAG, url);
                        b.putString(GetColumnsInfo.TITLE_TAG, title);
                        b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
                        b.putBoolean(GetColumnsInfo.HEADLINE_TAG, true);

                        itemintent.putExtras(b);
                        startActivity(itemintent);
                    } else { // 文章
                        Intent itemintent = new Intent(getActivity(), NewsPage.class);
                        Bundle b = new Bundle();
                        
                        List<Map<String, Object>> idlist = new ArrayList<Map<String, Object>>();
                        if(null != first){
                        	idlist.add(first);
                        }
                        if(null != mNewsList){
                        	idlist.addAll(mNewsList);
                        }
                        String idJsonString = StringUtil.createIdString(idlist);
                        if(!TextUtils.isEmpty(idJsonString)){
                        	b.putString(NewsFragment.ID_JSON_STRING, idJsonString);
                        }
                        
                        String docid = map.get(GetColumnsInfo.DOCID_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.DOCID_TAG));
                        String url = map.get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.URL_TAG));
                        String imgsrc = map.get(GetColumnsInfo.IMGSRC_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.IMGSRC_TAG));
                        String digest = map.get(GetColumnsInfo.DIGEST_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.DIGEST_TAG));
                        String replyCount = map.get(GetColumnsInfo.REPLYCOUNT_TAG) == null ? ""
                                : StringUtil.GetString(map.get(GetColumnsInfo.REPLYCOUNT_TAG));
                        String title = map.get(GetColumnsInfo.TITLE_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.TITLE_TAG));
                        String ptime = map.get(GetColumnsInfo.PTIME_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.PTIME_TAG));

                        b.putString(GetColumnsInfo.DOCID_TAG, docid);
                        b.putString(GetColumnsInfo.URL_TAG, url);
                        b.putString(GetColumnsInfo.IMGSRC_TAG, imgsrc);
                        b.putString(GetColumnsInfo.DIGEST_TAG, digest);
                        b.putString(GetColumnsInfo.REPLYCOUNT_TAG, replyCount);
                        b.putString(GetColumnsInfo.TITLE_TAG, title);
                        b.putString(GetColumnsInfo.PTIME_TAG, ptime);
                        b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
                        b.putString(GetColumnsInfo.TID_TAG, mColumnID);
                        b.putString(POSITION, "" + position);
                        b.putBoolean(GetColumnsInfo.HEADLINE_TAG, true);

                        itemintent.putExtras(b);
                        startActivity(itemintent);

                    }
                } else {
                    String tag = (String) map.get(GetColumnsInfo.NEWS_TAG);
                    if ("special".equals(tag)) { // 专题
                        Intent itemintent = new Intent(getActivity(), SpecialAritcleTwo.class);
                        Bundle b = new Bundle();

                        String title = (String) map.get(GetColumnsInfo.TITLE_TAG);
                        String sdocid = (String) map.get(GetColumnsInfo.URL_TAG);

                        b.putString(GetColumnsInfo.TITLE_TAG, title);
                        b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
                        b.putBoolean(GetColumnsInfo.HEADLINE_TAG, true);

                        itemintent.putExtras(b);
                        startActivity(itemintent);

                    } else if ("photoset".equals(tag)) { // 图集
                        Intent intent = new Intent(getActivity(), ImageSwitcherActivity.class);
                        Bundle bundle = new Bundle();

                        String column_setid = map.get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.URL_TAG));
                        
                        String colunm = column_setid.substring(0, column_setid.indexOf("|"));
                        
                        String setid = column_setid.substring(column_setid.indexOf("|") + 1);

                        String clientcover = map.get(GetColumnsInfo.IMGSRC_TAG) == null ? "" : StringUtil.GetString(map
                                .get(GetColumnsInfo.IMGSRC_TAG));
                        String imgTitle = map.get(GetColumnsInfo.TITLE_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.TITLE_TAG));
                        System.out.println("setid:"+setid+",clientcover:"+clientcover);
                        bundle.putString("setid", setid);
                        bundle.putString("channel", Tools.getChannel(colunm));
                        bundle.putString("imgsum", "0");
                        bundle.putString("imgTitle", imgTitle);
                        bundle.putString("clientcover", clientcover);
                        bundle.putBoolean("fromHead", true);
                        intent.putExtras(bundle);
                        startActivity(intent);

                    } else if ("doc".equals(tag)) { // 文章
                        Intent itemintent = new Intent(getActivity(), NewsPage.class);
                        Bundle b = new Bundle();

                        String docid = map.get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.URL_TAG));
                        String title = map.get(GetColumnsInfo.TITLE_TAG) == null ? "" : StringUtil.GetString(map.get(
                                GetColumnsInfo.TITLE_TAG));

                        b.putString(GetColumnsInfo.DOCID_TAG, docid);
                        b.putString(GetColumnsInfo.URL_TAG, docid);
                        b.putString(GetColumnsInfo.TITLE_TAG, title);
                        b.putString(GetColumnsInfo.TID_TAG, mColumnID);
                        b.putString(POSITION, "" + position);
                        b.putBoolean(GetColumnsInfo.HEADLINE_TAG, true);

                        itemintent.putExtras(b);
                        startActivity(itemintent);
                       
                    } else if ("link".equals(tag)) { // 其他链接
                        Intent itemintent = new Intent(getActivity(), LinkOutSide.class);
                        Bundle b = new Bundle();
                        String url = (String) map.get(GetColumnsInfo.URL_TAG);
                        b.putString("href", url);

                        itemintent.putExtras(b);
                        startActivity(itemintent);
                        
                    }
                }
            }
        });
    }

    /**
     * 获取头图滚动列表 具体数据保存在 SharedPreferences
     * 
     * @return
     */
    protected List<Map<String, Object>> getAdvertismentData() {
        List<Map<String, Object>> advData = new ArrayList<Map<String, Object>>();

        Context context = getActivity();

        SharedPreferences set_prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String advJsonStr = set_prefs.getString(ADVERTISMENT_JSON_STR, "");
        if (null != advJsonStr && advJsonStr.length() > 0) {

            try {
                JSONObject jobj = new JSONObject(advJsonStr);
                if (null != jobj) {
                    JSONArray advArray = jobj.getJSONArray("headline_ad");
                    if (null != advArray && advArray.length() > 0) {
                        int len = advArray.length();
                        for (int i = 0; i < len; i++) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            JSONObject obj = advArray.getJSONObject(i);
                            map.put(GetColumnsInfo.TITLE_TAG,
                                    obj.isNull("title") ? "" : obj.getString("title"));
                            map.put(GetColumnsInfo.SUBTITLE_TAG,
                                    obj.isNull("subtitle") ? "" : obj.getString("subtitle"));
                            map.put(GetColumnsInfo.NEWS_TAG,
                                    obj.isNull("tag") ? "" : obj.getString("tag"));
                            map.put(GetColumnsInfo.URL_TAG,
                                    obj.isNull("url") ? "" : obj.getString("url"));
                            String imgsrc = obj.isNull("imgsrc") ? "" : obj.getString("imgsrc");
                            map.put(GetColumnsInfo.IMGSRC_TAG, imgsrc);
                            
                            advData.add(map);
                        }
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
            }

        }

        return advData;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        boolean isHeadline = true;
        String docid = "";
        String url = "";
        String imgsrc = "";
        String digest = "";
        String replyCount = "";
        String title = "";
        String ptime = "";
        String sdocid = "";// 专题ID

        Context context = getActivity();
        String columnName = mColumnName;
        String columnID = mColumnID;

        Intent itemintent = new Intent(context, NewsPage.class);
        Bundle b = new Bundle();
        
        List<Map<String, Object>> idlist = new ArrayList<Map<String, Object>>();
        if(null != first){
        	idlist.add(first);
        }
        if(null != mNewsList){
        	idlist.addAll(mNewsList);
        }
        String idJsonString = StringUtil.createIdString(idlist);
        if(!TextUtils.isEmpty(idJsonString)){
        	b.putString(NewsFragment.ID_JSON_STRING, idJsonString);
        }
        
        try {
            b.putString(GetColumnsInfo.TNAME_TAG, columnName);
            if (isHeadline) {
                if (position == 0) {
                    // docid =
                    // first.get(GetColumnsInfo.DOCID_TAG)==null?"":first.get(GetColumnsInfo.DOCID_TAG).toString();
                    // url =
                    // first.get(GetColumnsInfo.URL_TAG)==null?"":first.get(GetColumnsInfo.URL_TAG).toString();
                    // imgsrc =
                    // first.get(GetColumnsInfo.IMGSRC_TAG)==null?"":first.get(GetColumnsInfo.IMGSRC_TAG).toString();
                    // digest =
                    // first.get(GetColumnsInfo.DIGEST_TAG)==null?"":first.get(GetColumnsInfo.DIGEST_TAG).toString();
                    // replyCount =
                    // first.get(GetColumnsInfo.REPLYCOUNT_TAG)==null?"":first.get(GetColumnsInfo.REPLYCOUNT_TAG).toString();
                    // title =
                    // first.get(GetColumnsInfo.TITLE_TAG)==null?"":first.get(GetColumnsInfo.TITLE_TAG).toString();
                    // ptime =
                    // first.get(GetColumnsInfo.PTIME_TAG)==null?"":first.get(GetColumnsInfo.PTIME_TAG).toString();
                    // sdocid =
                    // first.get(GetColumnsInfo.SPECIAL_ID_TAG)==null?"":first.get(GetColumnsInfo.SPECIAL_ID_TAG).toString();
                    return;
                } else {
                    List<Map<String, Object>> data = mNewsList;
                    docid = data.get(position - 1).get(GetColumnsInfo.DOCID_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.DOCID_TAG));
                    url = data.get(position - 1).get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(data
                            .get(position - 1).get(GetColumnsInfo.URL_TAG));
                    imgsrc = data.get(position - 1).get(GetColumnsInfo.IMGSRC_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.IMGSRC_TAG));
                    digest = data.get(position - 1).get(GetColumnsInfo.DIGEST_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.DIGEST_TAG));
                    replyCount = data.get(position - 1).get(GetColumnsInfo.REPLYCOUNT_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.REPLYCOUNT_TAG));
                    title = data.get(position - 1).get(GetColumnsInfo.TITLE_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.TITLE_TAG));
                    ptime = data.get(position - 1).get(GetColumnsInfo.PTIME_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.PTIME_TAG));
                    sdocid = data.get(position - 1).get(GetColumnsInfo.SPECIAL_ID_TAG) == null ? ""
                            : StringUtil.GetString(data.get(position - 1).get(GetColumnsInfo.SPECIAL_ID_TAG));
                }
            } else {
                // List<HashMap<String, Object>> listmap = (List<HashMap<String,
                // Object>>)dataMap4NonHead.get(columnID).getData();
                List<Map<String, Object>> listmap = mNewsList;
                if (listmap != null) {
                    docid = listmap.get(position).get(GetColumnsInfo.DOCID_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.DOCID_TAG));
                    url = listmap.get(position).get(GetColumnsInfo.URL_TAG) == null ? "" : StringUtil.GetString(listmap
                            .get(position).get(GetColumnsInfo.URL_TAG));
                    imgsrc = listmap.get(position).get(GetColumnsInfo.IMGSRC_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.IMGSRC_TAG));
                    digest = listmap.get(position).get(GetColumnsInfo.DIGEST_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.DIGEST_TAG));
                    replyCount = listmap.get(position).get(GetColumnsInfo.REPLYCOUNT_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.REPLYCOUNT_TAG));
                    title = listmap.get(position).get(GetColumnsInfo.TITLE_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.TITLE_TAG));
                    ptime = listmap.get(position).get(GetColumnsInfo.PTIME_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.PTIME_TAG));
                    sdocid = listmap.get(position).get(GetColumnsInfo.SPECIAL_ID_TAG) == null ? ""
                            : StringUtil.GetString(listmap.get(position).get(GetColumnsInfo.SPECIAL_ID_TAG));
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Log.i("Newsreader", "---------------------sdocid:"+sdocid);
        // 测试使用
        // sdocid = "S1310611586167";

        if (docid != null && !docid.equals("")) {
            // 标识为已读
            // setIsReaded4List(position, docid);

            b.putString(GetColumnsInfo.DOCID_TAG, docid);
            b.putString(GetColumnsInfo.URL_TAG, url);
            b.putString(GetColumnsInfo.IMGSRC_TAG, imgsrc);
            b.putString(GetColumnsInfo.DIGEST_TAG, digest);
            b.putString(GetColumnsInfo.REPLYCOUNT_TAG, replyCount);
            b.putString(GetColumnsInfo.TITLE_TAG, title);
            b.putString(GetColumnsInfo.PTIME_TAG, ptime);
            b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
            b.putString(GetColumnsInfo.TID_TAG, columnID);
            b.putString(POSITION, "" + position);
            b.putBoolean(GetColumnsInfo.HEADLINE_TAG, isHeadline);

            if (null != sdocid && !"".equals(sdocid)) {
                itemintent = new Intent(context, SpecialAritcleTwo.class);
            }
            itemintent.putExtras(b);
            startActivity(itemintent);
        } else if (null != sdocid && !"".equals(sdocid)) {
            itemintent = new Intent(context, SpecialAritcleTwo.class);
            b.putString(GetColumnsInfo.DOCID_TAG, docid);
            b.putString(GetColumnsInfo.URL_TAG, url);
            b.putString(GetColumnsInfo.IMGSRC_TAG, imgsrc);
            b.putString(GetColumnsInfo.DIGEST_TAG, digest);
            b.putString(GetColumnsInfo.REPLYCOUNT_TAG, replyCount);
            b.putString(GetColumnsInfo.TITLE_TAG, title);
            b.putString(GetColumnsInfo.PTIME_TAG, ptime);
            b.putString(GetColumnsInfo.SPECIAL_ID_TAG, sdocid);
            b.putString(GetColumnsInfo.TID_TAG, columnID);
            b.putString(POSITION, "" + position);
            b.putBoolean(GetColumnsInfo.HEADLINE_TAG, isHeadline);
            itemintent.putExtras(b);
            startActivity(itemintent);
        } else {
            Toast.makeText(context, "打开新闻正文失败", Toast.LENGTH_SHORT).show();
        }

    }

	@Override
	public void onApplyTheme() {
		super.onApplyTheme();

		ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
		Context context = getActivity();

		if (mHeadAdv != null) {
			themeSettingsHelper.setViewBackgroud(context,
					mHeadAdv.findViewById(R.id.top_bottom_space),
					R.drawable.top_text_background);
			themeSettingsHelper.setTextViewColor(context,
					(TextView) mHeadAdv.findViewById(R.id.text_title),
					R.color.top_text_color);
			mAdvGallery.setIndicator(context, themeSettingsHelper);
		}
	}
	
	private void preloadHeadPic(final BaseImageDownloader imageDownload ,final List<Map<String, Object>> adlist){
	    
	    new Thread(){
	        
	       public void run(){
	           if(null!=adlist && adlist.size()>0){
	               for(int index=0;index<adlist.size();index++){
	                   Map<String, Object> map = adlist.get(index);
	                   if(null!=map){
	                      String imgsrc = (String)map.get(GetColumnsInfo.IMGSRC_TAG);
	                      if(!TextUtils.isEmpty(imgsrc)){
	                          
	                         imgsrc = PicUtil.getHeandBitMapURL(imgsrc, mContext);
	                         imageDownload.getImageforBatche(imgsrc);
	                          
	                      }
	                   }
	               }
	           }
	       }
	       
	    }.start();
	    
	}

}
